import { _decorator, Color, Component, Node, Sprite, UITransform, v3, Vec3 } from 'cc';
const { ccclass, property } = _decorator;

import CardFunctions from "../Common/CardFunctions";
import { GameData } from "../Data/GameData";
import SS_TimerManager from "../Manager/SS_TimerManager";
import SS_NodeManager from "../Manager/SS_NodeManager";
import Block from "./Block";
import BlockGroup from "./BlockGroup";

@ccclass('Grid')
export default class Grid extends Component {
    //    /** 选中框节点 */
    @property(Node)
    select: Node | null = null;
    @property(Sprite)
    Sprite_main: Sprite | null = null;
    @property(Node)
    red: Node | null = null;
    id: number = 0;
    x: number = 0;
    y: number = 0;
    block: Block = null;
    canPlace: boolean = true;
    //    /** 卡牌颜色 */
    cardColor = [
        //        // "#e5ceab", //深色
        //        // "#f9e7c6", //浅色
        "#927566", //深色
        "#c5ac98", //浅色
    ]

    //    // 初始化
    init(_id: number, _x: number, _y: number) {
        this.id = _id;
        this.x = _x;
        this.y = _y;
        this.block = null;

        const colorIndes = (((this.y + 1) % 2) + ((this.x + 1) % 2)) % 2;
        // console.log(colorIndes);

        // this.Sprite_main.node.color = new Color().fromHEX(this.cardColor[colorIndes]);
        this.Sprite_main.node.getComponent(Sprite).color = new Color().fromHEX(this.cardColor[colorIndes]);

        this.Sprite_main.node.active = true;
        // this.Sprite_main.node.active = false;

        // console.log("GameData.cell_width, GameData.cell_height", GameData.cell_width, GameData.cell_height);

        this.initWidthAndHeight(GameData.cell_width, GameData.cell_height);
        this.initPosition();

        this.Refresh();
        this.AddListener();
    }

    RefreshPlace() {
        //        // this.X.active = !this.canPlace;
        //        // this.X.active = true;
    }

    GetNodeWorldPostation(_node: Node): Vec3 {
        let pos: Vec3 = v3();
        if (_node.parent == null) {
            pos = v3(_node.getPosition());
        } else {
            // pos = v3(_node.parent.convertToWorldSpaceAR(_node.getPosition()));
        }
        return pos;
    }

    async TOUCH_END() {

    }

    AddListener() {

    }

    Main() { }

    Refresh() {
        //        // this.node.active = this.id >= 0;
    }

    Select(select: boolean): void {
        this.select.active = select;
    }

    //    // 初始化单元宽高
    initWidthAndHeight(cardWidth?: number, cardHeight?: number) {
        if (cardWidth) this.node.getComponent(UITransform).width = cardWidth;
        if (cardHeight) this.node.getComponent(UITransform).height = cardHeight;
    }

    //    // 初始化位置
    initPosition() {
        let x = -GameData.W / 2 + this.node.getComponent(UITransform).width / 2 + this.node.getComponent(UITransform).width * this.x;
        let y = -GameData.H / 2 + this.node.getComponent(UITransform).height / 2 + this.node.getComponent(UITransform).height * this.y;
        this.node.position = v3(x, y, 0);
    }

    async Flash() {
        let duration = 0.2;
        this.red.active = true;
        await SS_TimerManager.AwaitTime(duration);
        this.red.active = false;

        await SS_TimerManager.AwaitTime(duration);
        this.red.active = true;
        await SS_TimerManager.AwaitTime(duration);
        this.red.active = false;

        await SS_TimerManager.AwaitTime(duration);
        this.red.active = true;
        await SS_TimerManager.AwaitTime(duration);
        this.red.active = false;
    }
}